package leetcode.editor.cn;

public class _152_MaximumProductSubarray {
    public static void main(String[] args) {
        Solution solution = new _152_MaximumProductSubarray().new Solution();
        int[] nums = new int[]{2, 3, -2, 4};
        int i = solution.maxProduct(nums);
        System.out.println(i);
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int maxProduct(int[] nums) {
            int res = nums[0], n = nums.length;
            int[] max = new int[n], min = new int[n];
            max[0] = min[0] = nums[0];
            for (int i = 1; i < n; i++) {
                min[i] = Math.min(nums[i], Math.min(nums[i] * min[i - 1], nums[i] * max[i - 1]));
                max[i] = Math.max(nums[i], Math.max(nums[i] * min[i - 1], nums[i] * max[i - 1]));
                res = Math.max(res, max[i]);
            }
            return res;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}